package com.yuyou.fn.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wens on 16-11-30.
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${dev.env}")
    private boolean dev;






    @Bean
    public Docket smsApi() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("短信接口").forCodeGeneration(true).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.regex("/api/sms/.*"))
                .build().apiInfo(apiInfo()).enable(dev);
    }

    @Bean
    public Docket adminApi() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("accessToken").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());

        return new Docket(DocumentationType.SWAGGER_2).groupName("后台接口").forCodeGeneration(true).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.regex("/api/admin/.*"))
                .build().apiInfo(apiInfo()).globalOperationParameters(new ArrayList<Parameter>(pars)).enable(dev);
    }


    @Bean
    public Docket qiniuApi() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("七牛上传token接口").forCodeGeneration(true).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.regex("/api/qiniu/.*"))
                .build().apiInfo(apiInfo()).enable(dev);
    }

    @Bean
    public Docket mApi() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("accessToken").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());

        return new Docket(DocumentationType.SWAGGER_2).groupName("移动端接口").forCodeGeneration(true).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.regex("/api/m/.*"))
                .build().apiInfo(apiInfo()).globalOperationParameters(new ArrayList<Parameter>(pars)).enable(dev);
    }

    @Bean
    public Docket serviceApi() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("服务平台接口").forCodeGeneration(true).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.regex("/api/service/.*"))
                .build().apiInfo(apiInfo()).enable(dev);
    }

    @Bean
    public Docket mobileApi() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("业务平台移动接口").forCodeGeneration(true).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.regex("/api/mobile/.*"))
                .build().apiInfo(apiInfo()).enable(dev);
    }

    @Bean
    public Docket costCenteApi() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("costCenter").forCodeGeneration(true).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.regex("/costCenter/.*"))
                .build().apiInfo(apiInfo()).enable(dev);
    }

    @Bean
    public Docket tasApi() {

        ParameterBuilder appIdPar = new ParameterBuilder();
        appIdPar.name("appId").description("appId")
                .modelRef(new ModelRef("long")).parameterType("header").required(true).build();
        ParameterBuilder timesPar = new ParameterBuilder();
        timesPar.name("times").description("时间戳")
                .modelRef(new ModelRef("long")).parameterType("header").required(true).build();
        ParameterBuilder signPar = new ParameterBuilder();
        signPar.name("sign").description("加密后的字符串:Md5(appId  +secretKey+ times).toLowerCase")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(true).required(true).build(); //header中的ticket参数非必填，传空也可以
        List<Parameter> pars = new ArrayList<Parameter>();
        pars.add(appIdPar.build());
        pars.add(timesPar.build());
        pars.add(signPar.build());



        return new Docket(DocumentationType.SWAGGER_2).groupName("tas").forCodeGeneration(true).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.regex("/tas/.*"))
                .build().apiInfo(apiInfo()).globalOperationParameters(pars).enable(dev);
    }



    private ApiInfo apiInfo() {
        Contact contact=new Contact("wens","www.google.com","wenzuojing@gmail.com");
        ApiInfo apiInfo = new ApiInfoBuilder().license("Apache License Version 2.0").title("誉优").description("誉优后台接口").contact(contact).version("1.0").build();
        return apiInfo;
    }
}
